/**
 * 
 */
package de.szut.dqi10.brailletouchscreenreader.display;

import de.szut.brailletouchscreenreader.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceFragment;
import android.util.Log;

/**
 * ActivityFragment that displays the Settings
 * @author Marco Jedzig, Simon Grafenhorst, Felix Pusch
 *
 */
public class BrailleSettingsActivityFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener, OnPreferenceClickListener {

	private static final String TAG = "BrailleSettingsActivityFragment";
	private static final int FILE_CHOOSER_REQUEST_CODE = 1;
	
	public static final String PREF_INPUTFILE_KEY = "pref_file";
	public static final String PREF_VIBRATEMODE_KEY = "pref_vibratemode";
	public static final String PREF_BUTTONSIZEX_KEY = "pref_buttonsizeX";
	public static final String PREF_BUTTONSIZEY_KEY = "pref_buttonsizeY";
	public static final String PREF_AUTONEXT_KEY = "pref_autonext";
	public static final String PREF_FULLSCREEN_KEY = "pref_fullscreen";
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Log.v(TAG, "Created a " + TAG);
		
		addPreferencesFromResource(R.xml.preferences);
		
		updatePreferenceSummaries();
	}

	/**
	 * Updates the summary texts of all affected preferences
	 */
	private void updatePreferenceSummaries() {
		SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
		String filePrefValue = sp.getString(PREF_INPUTFILE_KEY, "");
		Preference filePref = (Preference) findPreference(PREF_INPUTFILE_KEY);
		filePref.setSummary(filePrefValue);
		filePref.setOnPreferenceClickListener(this);
		
		ListPreference vibratemodePref = (ListPreference) findPreference(PREF_VIBRATEMODE_KEY);
		vibratemodePref.setSummary(vibratemodePref.getEntry());
		
		ListPreference buttonsizeXPref = (ListPreference) findPreference(PREF_BUTTONSIZEX_KEY);
		buttonsizeXPref.setSummary(buttonsizeXPref.getEntry());
		
		ListPreference buttonsizeYPref = (ListPreference) findPreference(PREF_BUTTONSIZEY_KEY);
		buttonsizeYPref.setSummary(buttonsizeYPref.getEntry());
	}
	
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		updatePreferenceSummaries();
	}
	
	@Override
	public void onResume() {
		super.onResume();
		Log.i(TAG, "resumed: " + TAG);
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}
	
	@Override
	public void onPause(){
		super.onPause();
		Log.i(TAG, "paused: " + TAG);
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		Log.i(TAG, "Clicked " + preference.toString() + " preference");
		startActivityForResult(new Intent(this.getActivity(), BrailleFileChooserActivity.class), FILE_CHOOSER_REQUEST_CODE);
		return true;
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		onResume();
		if (requestCode == FILE_CHOOSER_REQUEST_CODE) {
			if (resultCode == Activity.RESULT_OK) {
				String result = data.getStringExtra(BrailleFileChooserActivity.FILE_INTENT_EXTRA_TAG);
				Log.i(TAG, "Found BrailleFileChooserActivity result: " + result);
				
				SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
				Editor editor = sp.edit();
				editor.putString(PREF_INPUTFILE_KEY, result);
				editor.commit();
			}
		}
	}
}